package ldh.im.base.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import ldh.im.base.ChannelGroupManager;
import ldh.im.base.SessionManager;

@ChannelHandler.Sharable
public class ImSessionHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        SessionManager.getInstance().addChannel(ctx.channel());
        ChannelGroupManager.getInstance().addChannel(ctx.channel());
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        SessionManager.getInstance().remove(ctx.channel());
        ChannelGroupManager.getInstance().removeChannel(ctx.channel());
        ctx.fireChannelUnregistered();
    }
}
